import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;

public class CalculatorPanel extends JPanel {

	private static final long serialVersionUID = 8750675805162564446L;

	JFormattedTextField calculatorScreen;
	JPanel ButtonsGrid;
	
	int latestresult;
	
	int x = 0;
	
	int z = 0;
	
	int y = 0;

	public CalculatorPanel(int width, int height) {

		this.setSize(width, height);
		this.setLayout(null);

		Font font = loadFont().deriveFont((float) (height / 6));

		calculatorScreen = new JFormattedTextField(
				NumberFormat.getNumberInstance());
		calculatorScreen.setPreferredSize(new Dimension(width, height / 5));
		calculatorScreen.setBounds(0, 0, width, height / 5);
		calculatorScreen.setFont(font);
		calculatorScreen.setForeground(Color.BLACK);
		calculatorScreen.setBackground(new Color(0x709570));

		calculatorScreen.addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)
						&& (c != '+') && (c != '-') && (c != '/') && (c != '*')
						&& (c != '^')) {
					e.consume(); // ignore event
				}
			}
		});

		ButtonsGrid = new JPanel();
		ButtonsGrid.setBounds(0, height / 5, width, 400);
		ButtonsGrid.setLayout(new GridLayout(5, 5));
		for (int i = 0; i < 25; i++) {
			switch (i) {

			case 5: {
				JButton b = new JButton(7 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 6: {
				JButton b = new JButton(8 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 7: {
				JButton b = new JButton(9 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}

			case 10: {
				JButton b = new JButton(4 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 11: {
				JButton b = new JButton(5 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 12: {
				JButton b = new JButton(6 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 15: {
				JButton b = new JButton(1 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 16: {
				JButton b = new JButton(2 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}

			case 17: {
				JButton b = new JButton(3 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}

			case 21: {
				JButton b = new JButton(0 + "");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}

			case 22: {
				JButton b = new JButton("=");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						// call something set latest result
						latestresult = 100;
					}
				});
				break;
			}

			case 23: {
				JButton b = new JButton("+");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 18: {
				JButton b = new JButton("-");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 13: {
				JButton b = new JButton("*");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}
			case 8: {
				JButton b = new JButton("/");
				b.setFont(b.getFont().deriveFont((float) height / 8));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}

			case 1: {
				JButton b = new JButton("x");
				b.setFont(b.getFont().deriveFont((float) height / 8));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText()+" ");
					}
				});
				break;
			}
			case 2: {
				JButton b = new JButton("y");
				b.setFont(b.getFont().deriveFont((float) height / 8));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText()+" ");
					}
				});
				break;
			}
			
			case 3: {
				JButton b = new JButton("z");
				b.setFont(b.getFont().deriveFont((float) height / 8));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText()+" ");
					}
				});
				break;
			}

			case 4: {
				JButton b = new JButton("DEL");
				b.setFont(b.getFont().deriveFont((float) height / 15));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (calculatorScreen.getText().length() > 0) {
							calculatorScreen
									.setText(calculatorScreen.getText()
											.substring(
													0,
													calculatorScreen.getText()
															.length() - 1));
						}
					}
				});
				break;
			}
			
			case 9: {
				JButton b = new JButton("AC");
				b.setFont(b.getFont().deriveFont((float) height / 15));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						//Clear All
						calculatorScreen.setText("");
						latestresult = 0;
						x = 0;
						y = 0;
						z = 0;
					}
				});
				break;
			}

			case 24: {
				ImageIcon icon = new ImageIcon("icons/mic.png");
				JButton b = new JButton(icon);
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}

			case 14: {
				JButton b = new JButton("^");
				b.setFont(b.getFont().deriveFont((float) height / 7));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText());
					}
				});
				break;
			}

			case 19: {
				JButton b = new JButton("Log");
				b.setFont(b.getFont().deriveFont((float) height / 15));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText()+" ");
					
					}
				});
				break;
			}
			
			case 20: {
				JButton b = new JButton("ANS");
				b.setFont(b.getFont().deriveFont((float) height / 15));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						calculatorScreen.setText(latestresult+"");
					}
				});
				break;
			}
			
			case 0: {
				JButton b = new JButton("SET");
				b.setFont(b.getFont().deriveFont((float) height / 15));
				ButtonsGrid.add(b);
				setFocusable(false);
				b.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						JButton button = (JButton) e.getSource();
						calculatorScreen.setText(calculatorScreen.getText()
								+ button.getText() + " ");
						setFocusable(false);
					}
				});
				break;
			}
			
			default: {

				JButton b = new JButton();
				ButtonsGrid.add(b);
				b.setVisible(false);
				break;

			}

			}

		}

		add(calculatorScreen);
		add(ButtonsGrid);

		this.setVisible(true);

	}
	
	
	public void setScreenValue(int value){
		calculatorScreen.setText(value+"");
	}

	public Font loadFont() {
		try {
			return Font
					.createFont(Font.TRUETYPE_FONT, new File("fonts/DS-DIGIB.TTF"));
		} catch (FontFormatException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}

}
